package com.lion.uiwithzhihu.utils;

import java.util.List;

import com.activeandroid.Model;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.lion.model.ImageUrl;
import com.lion.model.Story;

public class DButils
{
	public static void collect(Story story)
	{
		
		story.save();
		List<String> images = story.getImages();
		if (images.size()>0)
		{
			ImageUrl imageUrl = new ImageUrl();
			imageUrl.setId(story.getStoryId());
			imageUrl.setImageUrl(story.getImages().get(0));
			imageUrl.save();
		}
	}
	
	public static  void delColect(Story story)
	{
		new Delete().from(Story.class).where("story_id=?", story.getStoryId()).execute();
	}
	public static boolean hasCollect(Story story)
	{
		Story storyCollect = new Select().from(Story.class).where("story_id=?",story.getStoryId()).executeSingle();
		
		return storyCollect!=null;
		/*return storyCollect!=null?true:false;*/
		
		/*if (storyCollect!=null)
		{
			return true;
		}else {
			return false;
		}*/
	}
	public static List<Story> queryCollect()
	{
		List<Story> data = new Select().from(Story.class).execute();
		for (int i = 0; i < data.size(); i++)
		{
			Story story = data.get(i);
			List<String> images = story.getImages();
			ImageUrl imageUrl = new Select().from(ImageUrl.class).where("story_id=?",story.getStoryId()).executeSingle();
			if (imageUrl!=null&&!"".equals(imageUrl))
			{
				String url = imageUrl.getImageUrl();
				images.add(url);
			}
		}
		return data;
		
	}
}
